package com.jdkj.tools.util;

import com.jdkj.tools.domain.TestCase;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.DateUtil;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

public class ParseExelUtil {

    /**
     * 读取 Excel 中的测试用例，遍历所有 sheet
     *
     * @param filePath   xlsx 文件路径
     * @param projectKey
     * @param projectId
     * @return List<TestCase>
     */
    public static void readTestCases(String filePath, String projectKey, String projectId)
            throws IOException {
        try (InputStream in = new FileInputStream(filePath);
             Workbook wb = WorkbookFactory.create(in)) {
            int numberOfSheets = wb.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                List<TestCase> list = new ArrayList<>();
                Sheet sheet = wb.getSheetAt(i);
                String sheetName = sheet.getSheetName();  //获得key\
                //创建文件夹
                String folderId = String.valueOf(CreateDirectory.init(projectKey, sheetName));

                Iterator<Row> rows = sheet.iterator();
                // 跳过每个 sheet 的表头
                if (rows.hasNext()) {
                    rows.next();
                }
                if (rows.hasNext()) {
                    rows.next();
                }
                if (rows.hasNext()) {
                    rows.next();
                }
                while (rows.hasNext()) {
                    Row row = rows.next();
                    // 如果“用例编号”这一列（index = 2）为空，则跳过
                    if (getCellValue(row.getCell(2)).isEmpty()) {
                        continue;
                    }
                    TestCase tc = new TestCase();
                    tc.setFeature(             getCellValue(row.getCell(0)));
                    tc.setSoftwareRequirement( getCellValue(row.getCell(1)));
                    tc.setCaseId(              getCellValue(row.getCell(2)));
                    tc.setTestPurpose(         getCellValue(row.getCell(3)));
                    tc.setPreCondition(        getCellValue(row.getCell(4)));
                    tc.setTestSteps(           getCellValue(row.getCell(5)));
                    tc.setExpectedResult(      getCellValue(row.getCell(6)));
                    tc.setTestResult(          getCellValue(row.getCell(7)));
                    tc.setTestData(            getCellValue(row.getCell(8)));
                    tc.setComment(             getCellValue(row.getCell(9)));
                    list.add(tc);
                }
                UpLoadUtil.UpLoadFile(projectKey,folderId,list,projectId);
            }
        }

    }

    /**
     * 通用的 Cell → String 转换
     */
    private static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue().trim();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    return new BigDecimal(cell.getNumericCellValue())
                            .toPlainString();
                }
            case BOOLEAN:
                return Boolean.toString(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            case BLANK:
                return "";
            default:
                return cell.toString().trim();
        }
    }

    @SneakyThrows
    public static void main(String[] args) {

    }
}
